package org.jeecg.modules.cw.service.impl;

import org.jeecg.modules.cw.entity.CwXzzcbzbTj;
import org.jeecg.modules.cw.entity.CwXzzcbzb;
import org.jeecg.modules.cw.mapper.CwXzzcbzbMapper;
import org.jeecg.modules.cw.mapper.CwXzzcbzbTjMapper;
import org.jeecg.modules.cw.service.ICwXzzcbzbTjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 新增资产配置提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-11
 * @Version: V1.0
 */
@Service
public class CwXzzcbzbTjServiceImpl extends ServiceImpl<CwXzzcbzbTjMapper, CwXzzcbzbTj> implements ICwXzzcbzbTjService {

	@Autowired
	private CwXzzcbzbTjMapper cwXzzcbzbTjMapper;
	@Autowired
	private CwXzzcbzbMapper cwXzzcbzbMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwXzzcbzbTj cwXzzcbzbTj, List<CwXzzcbzb> cwXzzcbzbList) {
		cwXzzcbzbTjMapper.insert(cwXzzcbzbTj);
		if(cwXzzcbzbList!=null && cwXzzcbzbList.size()>0) {
			for(CwXzzcbzb entity:cwXzzcbzbList) {
				//外键设置
				entity.setMainId(cwXzzcbzbTj.getId());
				cwXzzcbzbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwXzzcbzbTj cwXzzcbzbTj,List<CwXzzcbzb> cwXzzcbzbList) {
		cwXzzcbzbTjMapper.updateById(cwXzzcbzbTj);
		
		//1.先删除子表数据
		cwXzzcbzbMapper.deleteByMainId(cwXzzcbzbTj.getId());
		
		//2.子表数据重新插入
		if(cwXzzcbzbList!=null && cwXzzcbzbList.size()>0) {
			for(CwXzzcbzb entity:cwXzzcbzbList) {
				//外键设置
				entity.setMainId(cwXzzcbzbTj.getId());
				cwXzzcbzbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwXzzcbzbMapper.deleteByMainId(id);
		cwXzzcbzbTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwXzzcbzbMapper.deleteByMainId(id.toString());
			cwXzzcbzbTjMapper.deleteById(id);
		}
	}
	
}
